<?php
if (!defined('IN_DISCUZ')){
	exit('Access Denied');
}

function shop_menu($sid = 0, $showhide = FALSE, $evalue = FALSE){
	global $_G;

	if (!isset($_G['cache']['shops'])){
		loadcache('shops');
	}
	$shopcache = &$_G['cache']['shops'];
	$shoplist = array();
	$new_menu = array();

	if (!$sid){
		foreach ($shopcache as $shop_category){

			if (!$shop_category['status'] && !$showhide){
				continue;
			}

			if ($shop_category['type'] == 'group'){

				$shoplist[$shop_category['sid']]['name'] = $shop_category['name'];
				$visible[$shop_category['sid']] = true;

			}elseif ($shop_category['type'] == 'shop' && isset($visible[$shop_category['sup']]) && (!$shop_category['viewperm'] || ($shop_category['viewperm'] && forumperm($shop_category['viewperm'])) || strstr($shop_category['users'], "\t$_G[uid]\t"))){
				$shoplist[$shop_category['sup']]['sub'][$shop_category['sid']]['name'] = $shop_category['name'];
				$parent_shop[$shop_category['sid']]['parent'] = $shop_category['sup'];
				$visible[$shop_category['sid']] = true;

			}elseif ($shop_category['type'] == 'sub' && isset($visible[$shop_category['sup']]) && (!$shop_category['viewperm'] || ($shop_category['viewperm'] && forumperm($shop_category['viewperm'])) || strstr($shop_category['users'], "\t$_G[uid]\t"))){
				$shoplist[$parent_shop[$shop_category['sup']]['parent']]['sub'][$shop_category['sup']]['sub'][$shop_category['sid']]['name'] = $shop_category['name'];

			}
		}
		return $shoplist;
	}else{
		$sub = array();
		foreach ($shopcache as $category){
			if (!$category['status'] && !$showhide){
				continue;
			}
			if ($sid == $category['sup']){
				$new_menu[$category['sid']]['name'] = $category['name'];
				$sub[] = $category['sid'];
			}
			if (in_array($category['sup'], $sub)){
				$new_menu[$category['sup']]['sub'] = 'yes';
			}
		}
		return $new_menu;
	}
}

function loadshop(){
	global $_G;
	//Lay product id va shop category id
	$pid = intval($_G['gp_pid']);
	$sid = intval($_G['gp_sid']);

	if ($sid){
		$sid = is_numeric($sid) ? intval($sid) : (!empty($_G['setting']['shopsids'][$sid]) ? $_G['setting']['shopsids'][$sid] : 0);
	}

	//khoi tao kich thuoc cho anh thumb
	if ($_G['setting']['shoppicstyle']){
		$_G['setting']['shoppicstyle'] = unserialize($_G['setting']['shoppicstyle']);
		empty($_G['setting']['shoppicstyle']['thumbwidth']) && $_G['setting']['shoppicstyle']['thumbwidth'] = 214;
		empty($_G['setting']['shoppicstyle']['thumbheight']) && $_G['setting']['shoppicstyle']['thumbheight'] = 160;
	}else{
		$_G['setting']['shoppicstyle'] = array(
				'thumbwidth'=>214,
				'thumbheight'=>160
		);
	}

	$accessadd1 = $accessadd2 = $modadd1 = $modadd2 = $metadescription = '';
	$adminid = $_G['adminid'];

	if ($_G['uid']){
		if ($_G['member']['accessmasks']){
			$accessadd1 = ', a.allowview, a.allowpost, a.allowreply, a.allowgetattach, a.allowgetimage, a.allowpostattach, a.allowpostimage';
			$accessadd2 = "LEFT JOIN " . DB::table('shop_access') . " a ON a.uid='$_G[uid]' AND a.sid=s.sid";
		}

		if ($adminid == 3){
			$modadd1 = ', m.uid AS ismoderator';
			$modadd2 = "LEFT JOIN " . DB::table('shop_moderator') . " m ON m.uid='$_G[uid]' AND m.sid=s.sid";
		}
	}

	$product = '';
	if (!empty($pid) || !empty($sid)){
		if ($pid){
			$product = DB::fetch_first("SELECT p.*, d.name as province , m.*
											FROM " . DB::table('shop_products') . " p
											LEFT JOIN " . DB::table("common_district") . " d ON p.location_province = d.id
											LEFT JOIN " . DB::table("shop_profile") . " m ON p.uid = m.uid
											WHERE p.pid = '$pid'");
			if (!$product){
				showmessage(lang('shop/template', 'product_is_not_avaible'));
			}else{
				$product['tax'] = unserialize($product['tax']);
				$product['images'] = unserialize($product['images']);
			}

			$datachange = " view = view + 1 ";

			$product['view'] = $product['view'] + 1;
			DB::query("UPDATE " . DB::table('shop_products') . " SET $datachange WHERE pid = $pid");
		}

		if (!$sid && $product['cid'])
			$sid = $product['cid'];

		$shop_category = DB::fetch_first("SELECT s.*, ss.* $accessadd1 $modadd1
									FROM " . DB::table('shop_category') . " s
									LEFT JOIN " . DB::table("shop_shopfield") . " ss ON ss.sid=s.sid $accessadd2 $modadd2
									WHERE s.sid='$sid'");

		if ($shop_category){
			$shop_category['ismoderator'] = !empty($shop_category['ismoderator']) || $adminid == 1 || $adminid == 2 ? 1 : 0;
			$sid = $shop_category['sid'];

			foreach (array(
					'threadtypes',
					'threadsorts',
					'creditspolicy',
					'modrecommend',
					'param'
			) as $key){
				$shop_category[$key] = !empty($shop_category[$key]) ? unserialize($shop_category[$key]) : array();
				if (!is_array($shop_category[$key])){
					$shop_category[$key] = array();
				}
				if ($key == 'param' && $_G['gp_mod'] == 'post'){
					foreach ($shop_category['param'] as $key2=>$value){
						$shop_category['param'][$key2]['value'] = explode(chr(10), $value['value']);
					}
				}
			}
		}else
			$sid = 0;

	}

	$_G['sid'] = $sid;
	$_G['pid'] = $pid;
	$_G['shop'] = &$shop_category;
	$_G['product'] = &$product;
}

/*
 * List all products of categories
 * return array()
 * @author mr.Trieu
 * */
function productList($sid, $query = '', $sort = 'last', $start = 0, $limit = 12, &$count = '', $counter = FALSE, $formatPrice = TRUE){
	global $_G, $mod;

	$products = array();
	$mySelect = '';
	$mySql = "hide = 0 AND (p.pprice > '2000' OR p.uid = " . $_G['uid'] . ")";
	$myOrder = 'lastupdate DESC';

	if (intval($sid) >= 0){
		// Truong hop ko co category
		if ($_G['adminid'] != 1)
			$mySql .= " AND available = 1";
	}

	if (intval($sid) > 0){
		// Truong hop co category
		$id_list = loadChildCategory(shop_menu($sid));
		$catID = implode(',', $id_list);

		if ($catID)
			$mySql .= ' AND p.cid IN (' . $catID . ')';
		else
			$mySql .= ' AND p.cid = ' . $sid;
	}

	switch ($sort){
		case 'pasc':
			$myOrder = 'pprice ASC';
		break;
		case 'pdesc':
			$myOrder = 'pprice DESC';
		break;
		case 'sale':
			$mySql .= ' AND p.sales > 0 AND p.status = 1';
			$myOrder = 'sales DESC, lastupdate DESC';
		break;
	}

	if ($query){
		// For full text search
		if ($mod == 'search'){
			$mySelect .= ", $query AS score";
			$mySql .= " AND $query";
			$myOrder = 'score DESC';
		}else
			$mySql .= $query;
	}

	for ($c = 0; $c < 9; $c++){
		$mySelect .= ', custom_' . $c;
	}

	$sql = "SELECT p.pid, p.cid, p.uid, p.view, p.username, p.pname, p.punit, p.location_province, p.pcode, p.sales, p.state, p.b_sales, p.e_sales, p.total_vote, p.vote,
			p.img_cover, p.lastupdate, p.pprice, p.summary, p.tax, p.status, p.new, p.hot, p.available, c.param_number, m.*, d.name as province, a.attachment, a.remote" . $mySelect . "
			FROM " . DB::table("shop_products") . " p
			LEFT JOIN " . DB::table("shop_category") . " c ON p.cid = c.sid
			LEFT JOIN " . DB::table("common_district") . " d ON p.location_province = d.id
			LEFT JOIN " . DB::table("shop_products_attachment") . " a ON p.img_cover = a.aid
			LEFT JOIN " . DB::table("shop_profile") . " m ON p.uid = m.uid
			WHERE $mySql
			ORDER BY $myOrder
			LIMIT $start, $limit";
	//echo "<pre>$sql</pre>";


	if ($counter){
		$count = DB::result_first("SELECT COUNT(*) as total
									FROM " . DB::table("shop_products") . " p
									LEFT JOIN " . DB::table("common_district") . " d ON p.location_province = d.id
									LEFT JOIN " . DB::table("shop_products_attachment") . " a ON p.img_cover = a.aid
									LEFT JOIN " . DB::table("shop_profile") . " m ON p.uid = m.uid
									WHERE " . $mySql);
	}

	$res = DB::query($sql);

	while ($row = DB::fetch($res)){
		if (isset($row['tax']))
			$row['tax'] = unserialize($row['tax']);

		$row['images'] = unserialize($row['images']);
		$row['imgcover'] = ($row['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']) . 'shop/' . $row['attachment'];
		$row['imgcoverthumb'] = ($row['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']) . 'shop/' . substr($row['attachment'], 0, 10) . $row['img_cover'] . '.thumb.jpg';
		$row['imgcoverthumbbig'] = ($row['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']) . 'shop/' . substr($row['attachment'], 0, 10) . $row['img_cover'] . '.thumb.big.jpg';

		if ($row['sales'] > 0 && $row['status'] == 1){
			$row['sales_status'] = true;
			$row['end_price'] = $row['pprice'] - (($row['pprice'] * $row['sales']) / 100);
			$row['sales'] = FormatNumber($row['sales'], 0, '.', ",", FALSE);
		}else
			$row['end_price'] = $row['pprice'];

		$row['behind_tax'] = $row['tax']['used'] ? ($row['end_price'] + $row['delivery_price'] + ($row['end_price'] * $row['tax']['tax_vat']) / 100 + ($row['end_price'] * $row['tax']['tax_import']) / 100 + ($row['end_price'] * $row['tax']['tax_other']) / 100) : $row['end_price'];

		if ($formatPrice){
			$row['behind_tax'] = FormatNumber($row['behind_tax']);
			$row['save_money'] = FormatNumber($row['pprice'] - $row['end_price']);
			$row['pprice'] = FormatNumber($row['pprice']);
			$row['delivery_price'] = $row['delivery_price'] != 0 ? lang('shop/template', 'phi_van_chuyen') . FormatNumber($row['delivery_price']) : lang('shop/template', 'freeship');
			$row['end_price'] = FormatNumber($row['end_price']);
		}

		// Rate products
		$row['point'] = $row['number'] = $row['total_vote'] / $row['vote'];
		$row['point'] = round($row['point']) * 5;
		$row['number'] = round($row['number']);

		if ($row['view'] > 1000)
			$row['hits'] = round($row['view'] / 1000) . 'k';
		elseif ($row['view'] > 10000)
			$row['hits'] = round($row['view'] / 10000) . 'k';
		elseif ($row['view'] > 100000)
			$row['hits'] = round($row['view'] / 100000) . 'k';
		else
			$row['hits'] = $row['view'];

		// Param vs Summary
		if (!empty($row['param_number'])){
			for ($i = 0; $i < $row['param_number']; $i++){
				if (!empty($row['custom_' . $i])){
					$row['custom_' . $i] = unserialize($row['custom_' . $i]);

					foreach ($row['custom_' . $i] as $key=>$value){
						if ($key == 'name')
							$row['extra']['custom_' . $i]['name'] = $value;
						elseif ($key == 'custom'){
							if ($row['custom_' . $i]['custom']){
								$row['extra']['custom_' . $i]['custom'] = $value;
							}
						}
					}

					if ($row['custom_' . $i]['name']){
						unset($row['custom_' . $i]['name']);
						unset($row['custom_' . $i]['custom']);
						$row['extra']['custom_' . $i]['value'] = implode(', ', $row['custom_' . $i]);
					}

					unset($row['custom_' . $i]);
				}
			}

			if (!empty($row['extra'])){
				$row['isParam'] = true;

				foreach ($row['extra'] as $key=>$value){
					$row['str_param'] .= '<u>' . $value['name'] . '</u>: <b>' . $value['value'] . '</b>. ';
				}
			}
		}

		$products[] = $row;
	}

	return $products;
}

/*
 * Luot binh luan ve san pham
 * return number
 * */
function proComment($pid, $limit = 0){

	$comments = array();

	if ($limit > 0){
		$mysql = 'ORDER BY dateline DESC LIMIT 0,' . $limit;
	}else
		$mysql = 'ORDER BY dateline ASC';

	$Qry = "SELECT *
			FROM " . DB::table('home_comment') . "
			WHERE id = " . $pid . " AND idtype='product'
			$mysql";
	$res = DB::query($Qry);

	while ($com = DB::fetch($res)){
		$comments[] = $com;
	}

	return $comments;
}

/*
 * Cac shop ban cung 1 dung san pham
 * Tim kiem theo tu khoa la Ten San Pham
 * */
function shopBuy($cid, $pid, $keyword = ''){

	global $_G;
	$products = array();
	$compare = false;

	// Kiem tra category co cho phep so sanh gia hay ko
	if (!isset($_G['cache']['shops'])){
		loadcache('shops');
	}
	$shopcache = &$_G['cache']['shops'];

	foreach ($shopcache as $key=>$value){
		if ($key == $cid){
			if ($value['compare'] == 1){
				$compare = true;
			}
		}
	}

	if ($compare){
		// Check and fix keyword
		if ($keyword == '')
			return false;

		$quote = array(
				'"',
				"'",
				'\"',
				"\'",
				'&quot;',
				'`'
		);
		$keyword = trim($keyword);
		$keyword = str_replace($quote, '', $keyword);

		$str = "MATCH (pname) AGAINST ('\"" . $keyword . "\"' IN BOOLEAN MODE)";

		$mySelect = ', ' . $str . ' AS score';
		//	$mySql	  = "p.cid = $cid AND p.pid != $pid AND (p.pprice > '2000' OR p.uid = '$_G[uid]')";
		$mySql = "p.cid = $cid AND hide = 0 AND (p.pprice > '2000' OR p.uid = '$_G[uid]')";
		$myOrder = 'score DESC';

		if ($_G['adminid'] != 1)
			$mySql .= " AND available = 1";

		$mySql .= ' AND ' . $str;

		$sql = "SELECT p.pid, p.uid, p.username, p.pname, p.punit, p.state, p.total_vote, p.vote, p.delivery_price,
			p.lastupdate, p.pprice, p.status, m.*, d.name as province" . $mySelect . "
			FROM " . DB::table("shop_products") . " p
			LEFT JOIN " . DB::table("common_district") . " d ON (p.location_province = d.id)
			LEFT JOIN " . DB::table("shop_products_attachment") . " a ON (p.img_cover = a.aid)
			LEFT JOIN " . DB::table("shop_profile") . " m ON (p.uid = m.uid)
			WHERE $mySql
			ORDER BY $myOrder";
		//echo "<pre>$sql</pre>";


		$res = DB::query($sql);

		while ($row = DB::fetch($res)){

			if ($row['sales'] > 0 && $row['status'] == 1)
				$row['end_price'] = $row['pprice'] - (($row['pprice'] * $row['sales']) / 100);
			else
				$row['end_price'] = $row['pprice'];

			$row['end_price'] = FormatNumber($row['end_price']);
			$row['shipping'] = $row['delivery_price'] != 0 ? FormatNumber($row['delivery_price']) : lang('shop/template', 'mien_phi');

			// Rate products
			$row['point'] = $row['number'] = $row['total_vote'] / $row['vote'];
			$row['point'] = round($row['point']) * 5;
			$row['number'] = round($row['number']);

			$row['shop_rate'] = round($row['rate'] * 5);

			$row['comment'] = proComment($row['pid']);
			$row['comment'] = count($row['comment']);

			$products[] = $row;
		}
	}

	return $products;
}

/*
 * San pham dong gia
 * Gia san pham nam trong khoangr lowPrice < currentPrice < hightPrice
 * */
function samePrice($cid, $pid, $pprice, $limit = 10){

	global $_G;
	$products = array();
	$mySql = '';

	if (!$cid || !$pid || !$pprice || !is_numeric($pprice))
		return false;

	$lowPrice = 0;
	$hightPrice = $pprice;

	if ($pprice < 10000)
		$lowPrice = $pprice - 2000;
	elseif ($pprice >= 100000 && $pprice < 500000)
		$lowPrice = $pprice - 20000;
	elseif ($pprice >= 500000 && $pprice < 1000000)
		$lowPrice = $pprice - 50000;
	elseif ($pprice >= 1000000 && $pprice < 5000000)
		$lowPrice = $pprice - 100000;
	elseif ($pprice >= 5000000 && $pprice < 10000000)
		$lowPrice = $pprice - 1000000;
	elseif ($pprice >= 10000000)
		$lowPrice = $pprice - 1000000;

	if ($lowPrice <= 0)
		$lowPrice = 2000; // Muc gia toi thieu theo quy dinh luc dang san pham moi.


	if ($_G['adminid'] != 1)
		$mySql = " AND available = 1";

		//p.cid = ' . $cid . ' AND
	$Qry = 'SELECT p.pid, p.pname, p.img_cover, a.attachment, p.pprice, p.sales, p.status
			FROM ' . DB::table("shop_products") . ' p
			LEFT JOIN ' . DB::table("shop_products_attachment") . ' a ON (p.img_cover = a.aid)
			WHERE p.cid = ' . $cid . ' AND p.pid != ' . $pid . ' AND hide = 0 AND (p.pprice BETWEEN ' . $lowPrice . ' AND ' . $hightPrice . ')' . $mySql . '
			ORDER BY p.pprice ASC
			LIMIT 0, ' . $limit;
	//echo $Qry;
	$res = DB::query($Qry);

	while ($row = DB::fetch($res)){
		$row['imgcoverthumb'] = ($row['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']) . 'shop/' . substr($row['attachment'], 0, 10) . $row['img_cover'] . '.thumb.jpg';

		if ($row['sales'] > 0 && $row['status'] == 1){
			$row['sales_status'] = true;
			$row['end_price'] = $row['pprice'] - (($row['pprice'] * $row['sales']) / 100);
			$row['sales'] = FormatNumber($row['sales'], 0, '.', ",", FALSE);
		}else
			$row['end_price'] = $row['pprice'];

		$row['end_price'] = FormatNumber($row['end_price']);

		$products[] = $row;
	}

	return $products;
}

/*
 * Tim kiem san pham theo thuoc tinh
 * lay thuoc tinh dau tien cua san pham dang xem
 * de loc ket qua
 * */
function searchFirstKey($cid, $pid, $param = array()){
	global $_G;
	$products = array();
	$mySql = "hide = 0 AND p.cid = $cid AND p.pid != $pid AND p.pprice > 2000";

	if (!is_array($param))
		return false;

	$mySql .= ' AND MATCH (custom_0) AGAINST (';

	$mySql .= '\'"' . $param['value'] . '"\' ';

	$mySql .= 'IN BOOLEAN MODE)';

	if ($_G['adminid'] != 1)
		$mySql .= " AND available = 1";

	$Qry = 'SELECT p.pid, p.pname, p.img_cover, a.attachment, p.pprice, p.sales, p.status
			FROM ' . DB::table("shop_products") . ' p
			LEFT JOIN ' . DB::table("shop_products_attachment") . ' a ON (p.img_cover = a.aid)
			WHERE ' . $mySql . '
			ORDER BY lastupdate ASC';
	//	echo $Qry;


	$res = DB::query($Qry);

	while ($row = DB::fetch($res)){
		$row['imgcoverthumb'] = ($row['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']) . 'shop/' . substr($row['attachment'], 0, 10) . $row['img_cover'] . '.thumb.jpg';

		if ($row['sales'] > 0 && $row['status'] == 1){
			$row['sales_status'] = true;
			$row['end_price'] = $row['pprice'] - (($row['pprice'] * $row['sales']) / 100);
			$row['sales'] = FormatNumber($row['sales'], 0, '.', ",", FALSE);
		}else
			$row['end_price'] = $row['pprice'];

		$row['end_price'] = FormatNumber($row['end_price']);

		$products[] = $row;
	}

	return $products;
}

/*
 * San pham cung danh muc
 * */
function inCategory($cid, $pid, $limit = 10){

	global $_G;
	$products = array();

	$mySql = "hide = 0 AND p.cid = $cid AND p.pid != $pid";

	if ($_G['adminid'] != 1)
		$mySql .= " AND available = 1";

	$Qry = 'SELECT p.pid, p.pname, p.img_cover, a.attachment, p.pprice, p.sales, p.status, p.summary, p.lastupdate, p.view
			FROM ' . DB::table("shop_products") . ' p
			LEFT JOIN ' . DB::table("shop_products_attachment") . ' a ON (p.img_cover = a.aid)
			WHERE ' . $mySql . '
			ORDER BY lastupdate ASC
			LIMIT 0, ' . $limit;
	//echo $Qry;


	$res = DB::query($Qry);

	while ($row = DB::fetch($res)){
		$row['imgcoverthumb'] = ($row['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']) . 'shop/' . substr($row['attachment'], 0, 10) . $row['img_cover'] . '.thumb.jpg';

		if ($row['sales'] > 0 && $row['status'] == 1){
			$row['sales_status'] = true;
			$row['end_price'] = $row['pprice'] - (($row['pprice'] * $row['sales']) / 100);
			$row['sales'] = FormatNumber($row['sales'], 0, '.', ",", FALSE);
		}else
			$row['end_price'] = $row['pprice'];

		$row['end_price'] = FormatNumber($row['end_price']);

		if ($row['view'] > 1000)
			$row['hits'] = round($row['view'] / 1000) . 'k';
		elseif ($row['view'] > 10000)
			$row['hits'] = round($row['view'] / 10000) . 'k';
		elseif ($row['view'] > 100000)
			$row['hits'] = round($row['view'] / 100000) . 'k';
		else
			$row['hits'] = $row['view'];

		$products[] = $row;
	}

	return $products;
}

/*
 * Cac san pham khac cung shop dang ban
 * */
function sameShop($uid, $cid, $pid, $limit = 8){
	global $_G;
	$products = array();

	$mySql = "hide = 0 AND p.cid = $cid AND p.pid != $pid AND p.pprice > 2000 AND p.uid = " . $uid;

	if ($_G['adminid'] != 1)
		$mySql .= " AND available = 1";

	$Qry = "SELECT p.pid, p.cid, p.uid, p.view, p.pname, p.img_cover, p.pprice, p.summary, p.lastupdate, p.sales, p.status, a.attachment, a.remote
			FROM " . DB::table("shop_products") . " p
			LEFT JOIN " . DB::table("common_district") . " d ON (p.location_province = d.id)
			LEFT JOIN " . DB::table("shop_products_attachment") . " a ON (p.img_cover = a.aid)
			WHERE $mySql
			ORDER BY lastupdate DESC
			LIMIT 0, $limit";
	//echo "<pre>$Qry</pre>";


	$products = arrayFormat($Qry);

	return $products;
}

/*
 * Hot categories
 * */
function category_hot(){
	global $_G;

	if (!isset($_G['cache']['shops'])){
		loadcache('shops');
	}
	$shopcache = &$_G['cache']['shops'];

	$catname = array();
	$catid = array();
	$shoplist = array();

	foreach ($shopcache as $key=>$value){
		if ($value['hot'] == 1){
			$catid[] = $key;
			$catname[] = $value['name'];
		}
	}

	$shoplist = array_combine($catid, $catname);

	return $shoplist;
}

/*
 * Hot sub categories
 * */
function subcategory_hot(){
	global $_G;

	if (!isset($_G['cache']['shopsub'])){
		loadcache('shopsub');
	}
	$shopcache = &$_G['cache']['shopsub'];

	return $shopcache;
}

/*
 * San pham gia Shock
 * */
function spShockIndex($item = 0){
	global $_G;
	$products = array();

	if (!isset($_G['cache']['hotDeal'])){
		loadcache('hotDeal');
	}
	$products = &$_G['cache']['hotDeal'];

	if ($item)
		return array_slice($products, 0, $item);
	else
		return $products;
}

/*
 * San pham moi
 * */
function spNew($item = 0){
	global $_G;
	$products = array();

	if (!isset($_G['cache']['proNew'])){
		loadcache('proNew');
	}
	$products = &$_G['cache']['proNew'];

	if ($item)
		return array_slice($products, 0, $item);
	else
		return $products;
}

/*
 * San pham noi bat
 * */
function spHot($item = 0){
	global $_G;
	$products = array();

	if (!isset($_G['cache']['proHot'])){
		loadcache('proHot');
	}
	$products = &$_G['cache']['proHot'];

	if ($item)
		return array_slice($products, 0, $item);
	else
		return $products;
}

/*
 * San pham gia tot moi ngay
 * */
function spBestPrice($item = 0){
	global $_G;
	$products = array();

	if (!isset($_G['cache']['bestPrice'])){
		loadcache('bestPrice');
	}
	$products = &$_G['cache']['bestPrice'];

	if ($item)
		return array_slice($products, 0, $item);
	else
		return $products;
}

/*
 * Save cookie
 * for history viewer
 * History in a week
 * */
function proViewCookie($id){
	$lastTime = strtotime('now');
	$proExist = 0;
	$proViewCookie = '';

	if (isset($_COOKIE['proViewCookie'])){
		$myArray = explode('|', trim($_COOKIE['proViewCookie']));
		$totalCook = count($myArray);

		for ($i = 0; $i < $totalCook - 1; $i = $i + 2){
			if ($myArray[$i] == $id){
				$proViewCookie .= $myArray[$i] . '|' . $myArray[$i + 1] . '|';
				$proExist = 1;
			}else
				$proViewCookie .= $myArray[$i] . '|' . $myArray[$i + 1] . '|';
		}
	}
	//ID chua ton tai trong cookie
	if ($proExist == 0)
		$proViewCookie .= $id . '|' . $lastTime . '|';

	$proViewCookie = substr($proViewCookie, 0, strlen($proViewCookie) - 1);
	// Set cookie [proViewCookie]
	setcookie('proViewCookie', $proViewCookie, time() + 604800, '/');
}

/*
 * Check history viewer
 * in a week
 * */
function checkProViewCookie($history = ''){
	$products = array();

	if (isset($_COOKIE['proViewCookie'])){

		$startDate = mktime(date(0), date(0), date(0), date('n'), date('d'), date('Y'));
		$endDate = mktime(date(23), date(59), date(59), date('n'), date('d'), date('Y'));

		$todayArray = '';
		$agoArray = '';
		$aWeek = '';
		$myArray = explode('|', trim($_COOKIE['proViewCookie']));
		$totalCook = count($myArray);

		for ($i = 0; $i < $totalCook - 1; $i = $i + 2){
			// Tat ca sp da xem
			$aWeek .= $myArray[$i] . ',';
			// Nhung sp da xem ngay hom nay
			if ($myArray[$i + 1] >= $startDate && $myArray[$i + 1] <= $endDate)
				$todayArray .= $myArray[$i] . ',';

		// Nhung sp da xem trong tuan
			elseif ($myArray[$i + 1] < $startDate)
				$agoArray .= $myArray[$i] . ',';
		}

		$aWeek = substr($aWeek, 0, strlen($aWeek) - 1);
		$todayArray = substr($todayArray, 0, strlen($todayArray) - 1);
		$agoArray = substr($agoArray, 0, strlen($agoArray) - 1);

		// today and more
		if ($history == 'today'){
			var_dump($todayArray);
			echo '<br />';
			var_dump($agoArray);
		}else{
			$Qry = 'SELECT p.pid, p.pname, p.img_cover, a.attachment, p.pprice, p.sales, p.view, p.summary, p.lastupdate, p.status, m.uid, m.shopname
					FROM ' . DB::table("shop_products") . ' p
					LEFT JOIN ' . DB::table("shop_products_attachment") . ' a ON (p.img_cover = a.aid)
					LEFT JOIN ' . DB::table("shop_profile") . ' m ON p.uid = m.uid
					WHERE available = 1 AND p.pid IN(' . $aWeek . ')
					ORDER BY p.lastupdate DESC';

			$products = arrayFormat($Qry);
		}
	}

	return $products;
}

/*
 * Array format for products
 * */
function arrayFormat($Qry = ''){
	global $_G;

	$res = DB::query($Qry);

	while ($row = DB::fetch($res)){
		$row['imgcoverthumb'] = ($row['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']) . 'shop/' . substr($row['attachment'], 0, 10) . $row['img_cover'] . '.thumb.jpg';

		if ($row['sales'] > 0 && $row['status'] == 1){
			$row['sales_status'] = true;
			$row['end_price'] = ($row['pprice'] - (doubleval($row['pprice']) * doubleval($row['sales'])) / 100);
			$row['end_price'] = FormatNumber($row['end_price']);
			$row['sales'] = FormatNumber($row['sales'], 0, '.', '', FALSE);
		}else
			$row['end_price'] = FormatNumber($row['pprice'], 0, '.', '');

		$product[] = $row;
	}

	return $product;
}

/*
 * Get province of Vietnam
 * */
function provice(){
	global $_G;
	$provice = array();

	if (!isset($_G['cache']['vnprovice'])){
		loadcache('vnprovice');
	}
	$provice = &$_G['cache']['vnprovice'];

	return $provice;
}

/*
 * Generate code
 * for Product code
 * */
function createRandomCode(){
	$chars = "abcdefghiljkmnpqrstuvwxyz123456789";
	srand((double)microtime() * 1000000);
	$i = 0;
	$pass = '';
	while ($i <= 7){
		$num = rand() % 33;
		$tmp = substr($chars, $num, 1);
		$pass = $pass . $tmp;
		$i++;
	}
	return strtoupper($pass);
}

function shopcode($username){
	return strtoupper(substr($username, 0, 2) . substr(md5($username), 0, 3));
}

/*
 * Shop profile
 * */
function check_profile_shop($uid){
	$profile = NULL;
	$profile = DB::fetch_first("SELECT * FROM " . DB::table("shop_profile") . " p WHERE p.uid= '" . $uid . "'");
	return $profile;
}

function get_str_nohtml($string){
	if (is_array($string)){
		foreach ($string as $key=>$val){
			$string[$key] = get_str_nohtml($val);
		}
	}else{
		$string = trim(str_replace(array(
				"'",
				'"',
				'<',
				'>',
				'\t'
		), array(
				'&rsquo;',
				'&quot;',
				'&lt;',
				'&gt;',
				' '
		), $string));
	}
	return pdstripcslashes($string);
}

function pdstripcslashes($string){
	if (is_array($string)){
		foreach ($string as $key=>$val){
			$string[$key] = pdstripcslashes($val);
		}
	}else{
		$string = stripcslashes($string);
	}
	return $string;
}

/*
 * for og:image usage
 * */
function getattachURL($attach, $folder = '', $thumb = 0){
	global $_G;
	$URL = '';

	$attdir = $URL .= $attach['remote'] ? $_G['setting']['ftp']['attachurl'] . 'shop/' : $_G['setting']['attachurl'] . $folder;

	switch ($thumb){
		case 1:
			$URL .= $attach['attachment'];
		break;
		case 2:
			$URL .= substr($attach['attachment'], 0, 10);
			$URL .= $attach['aid'] . '.thumb.big.jpg';
		break;
		default:
			$URL .= substr($attach['attachment'], 0, 10);
			$URL .= $attach['aid'] . '.thumb.jpg';
		break;
	}

	return $URL;
}

/*
 * Search products for attribude
 * @author mr.Trieu
 * */
function attrArray($str = ''){
	$arrays = array();
	$nArray = array();

	// $str to array()
	if (substr($str, strlen($str) - 1, 1) == chr(64))
		$str = substr($str, 0, strlen($str) - 1);

	$param = explode(chr(64), $str);

	foreach ($param as $value){
		$n_array = explode(chr(124), $value);
		foreach ($n_array as $value2){
			$arrays[$n_array[0]][] = $value2;
		}
	}

	foreach ($arrays as $value){
		$value = array_unique($value);
		$nArray[] = implode(chr(124), $value);
	}

	return $nArray;
}

/*
 * Get all attridude of categories
 * @author mr.Trieu
 * return string
 * */
function listAttr($sid){

	global $_G;
	$valArray = array();
	$attrArray = array();

	if ($sid){
		$allParam = $_G['shop']['param'];
		$curl = $_SERVER['REQUEST_URI'];
		$needsapce = array(
				' ',
				'&nbsp;',
				'&#32;'
		);
		$needand = array(
				'&',
				'&amp;',
				'&#38;'
		);

		foreach ($allParam as $key=>$value){
			$attrTitle = trim(str_replace($needand, chr(38), $value['name']));
			$attrTitle = str_replace($needsapce, chr(32), $attrTitle);

			$values = explode("\n", $value['value']);

			foreach ($values as $text){
				$title = trim(str_replace($needand, chr(38), $text));
				$title = str_replace($needsapce, chr(32), $title);

				$valArray[] = $title;
			}

			$attrArray[] = array(
					$attrTitle=>$valArray
			);
			unset($valArray);
		}
	}

	return $attrArray;
}

/*
 * Search produtcts for attribude
 * @author mr.Trieu
 * */
function searchAttr($attrArray = array(), $custom = 9){

	if (empty($attrArray))
		return false;

	$quote = array(
			'"',
			"'",
			'\"',
			"\'",
			'&quot;',
			'`'
	);

	$str = 'MATCH (';

	for ($q = 0; $q <= $custom; $q++){
		$str .= 'custom_' . $q . ', ';
	}

	if (substr($str, strlen($str) - 2, 2) == ', ')
		$str = substr($str, 0, strlen($str) - 2);

	$str .= ') AGAINST (';

	foreach ($attrArray as $key=>$values){
		$attr = explode('_', $values);
		unset($attr[0]);

		foreach ($attr as $key=>$value){
			$str .= '\'"' . str_replace($quote, '', $value) . '"\' ';
		}
	}

	$str .= 'IN BOOLEAN MODE)';
	return $str;
}

function paserimgatt($detail){
	global $_G;

	if (preg_match_all("/\[attachimg\](\d+)\[\/attachimg\]/i", $detail, $matchaids)){
		$attachlist = $matchaids[1];
		$attachlist = implode(',', $attachlist);
		$query = DB::query("SELECT * FROM " . DB::table("shop_products_attachment") . " WHERE `aid` IN ($attachlist)");
		$attachlist = '';

		while ($attach = DB::fetch($query)){
			$attach['imgurl'] = $attach['remote'] ? $_G['setting']['ftp']['attachurl'] . 'shop/' : $_G['setting']['attachurl'] . 'shop/';
			$attach['imgurl'] = $attach['imgurl'] . $attach['attachment'];
			$attachlist[$attach['aid']] = $attach;
		}
		foreach ($attachlist as $key=>$value){
			$search = '[attachimg]' . $key . '[/attachimg]';
			$replace = reg_replace_images($value);
			$detail = str_replace($search, $replace, $detail);
		}
	}
	return $detail;
}

function reg_replace_images($attach){
	$width = '';

	if ($attach['width'] > 760)
		$width = 'width = "760px" style="cursor:pointer" alt="View full size" title="View full size"';

	$string = '<img id="aimg_' . $attach['aid'] . '" class="center" src="' . $attach['imgurl'] . '" zoomfile="' . $attach['imgurl'] . '" file="' . $attach['imgurl'] . '" class="zoom" onclick="zoom(this, this.src)" ' . $width . ' inpost="1" />';
	return $string;
}

/*
 * For attachment
 * */
function getattachshop($pid, $posttime = 0, $aids = ''){
	global $_G;

	require_once libfile('function/attachment');
	$attachs = $imgattachs = array();
	$aids = $aids ? explode('|', $aids) : array();

	if ($aids){
		$aidsnew = array();
		foreach ($aids as $aid){
			if ($aid){
				$aidsnew[] = intval($aid);
			}
		}
		$aids = "aid IN (" . dimplode($aidsnew) . ") AND";
	}else{
		$aids = '';
	}

	$sqladd1 = $posttime > 0 ? "AND af.dateline>'$posttime'" : '';

	if (!empty($_G['sid']) && $_G['shop']['attachextensions']){
		$allowext = str_replace(' ', '', strtolower($_G['shop']['attachextensions']));
		$allowext = explode(',', $allowext);
	}else{
		$allowext = '';
	}

	$query = DB::query("SELECT a.*, af.*
						FROM " . DB::table('forum_attachment') . " a
						LEFT JOIN " . DB::table('forum_attachment_unused') . " af USING(aid)
						WHERE $aids (af.uid='$_G[uid]' AND a.tid='0' $sqladd1) ORDER BY a.aid DESC");

	while ($attach = DB::fetch($query)){
		$attach['filenametitle'] = $attach['filename'];
		$attach['ext'] = strtolower(fileext($attach['filename']));
		if ($allowext && !in_array($attach['ext'], $allowext)){
			continue;
		}
		getattach_img_thumb($attach, $attachs, $imgattachs);
	}

	if ($pid > 0){
		$query = DB::query("SELECT * FROM " . DB::table("shop_products_attachment") . " WHERE `pid` = $pid");
		while ($attach = DB::fetch($query)){
			$attach['filenametitle'] = $attach['filename'];
			$attach['ext'] = fileext(strtolower($attach['filename']));
			getattach_img_thumb($attach, $attachs, $imgattachs);
		}
	}
	return array(
			'attachs'=>$attachs,
			'imgattachs'=>$imgattachs
	);
}

function getattach_img_thumb($attach, &$attachs, &$imgattachs){
	global $_G;

	$attach['filename'] = cutstr($attach['filename'], $_G['setting']['allowattachurl'] ? 25 : 30);
	$attach['attachsize'] = sizecount($attach['filesize']);
	$attach['dateline'] = dgmdate($attach['dateline']);
	$attach['filetype'] = attachtype($attach['ext'] . "\t" . $attach['filetype']);

	if ($attach['isimage'] < 1){
		if ($attach['isimage']){
			$attach['url'] = $attach['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl'];
			$attach['width'] = $attach['width'] > 300 ? 300 : $attach['width'];
		}
		if ($attach['pid']){
			$attachs['used'][] = $attach;
		}else{
			$attachs['unused'][] = $attach;
		}
	}else{
		$attach['url'] = ($attach['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']) . 'shop';
		$attach['width'] = $attach['width'] > 300 ? 300 : $attach['width'];

		if ($attach['pid']){
			$imgattachs['used'][] = $attach;
		}else{
			$imgattachs['unused'][] = $attach;
		}
	}
}

function punlink($attach, $action = 'insert', $thumb = FALSE){
	global $_G;
	$filename = $attach['attachment'];
	$havethumb = $attach['thumb'];
	$remote = $attach['remote'];
	$dir_src = $action == 'insert' ? '/forum/' : '/shop/';

	if ($remote){
		ftpcmd('delete', $_G['setting']['ftp']['attachdir'] . $dir_src . $filename);

		//		$havethumb && ftpcmd('delete', $_G['setting']['ftp']['attachdir'] . $dir_src . getimgthumbname($filename));
	}else{
		@unlink($_G['setting']['attachdir'] . $dir_src . $filename);

		//		$havethumb && @unlink($_G['setting']['attachdir'] . $dir_src . getimgthumbname($filename));
	}
	//	if ($attach['aid']){
	//		@unlink($_G['setting']['attachdir'] . 'image/' . $attach['aid'] . '_140_140.jpg');
	//	}
	if ($thumb){
		@unlink($_G['setting']['attachdir'] . $dir_src . substr($attach['attachment'], 0, 10) . '/' . $attach['aid'] . ".thumb.jpg");
		@unlink($_G['setting']['attachdir'] . $dir_src . substr($attach['attachment'], 0, 10) . '/' . $attach['aid'] . ".thumb.big.jpg");
	}
}

/*******************************************************************************
 * For Product property
 ******************************************************************************/
function param_explode($array){
	$output = '';
	$isValue = array();
	$i = 0;

	if ($pparam){
		$isValue = $pparam;
	}

	foreach ($array as $key){
		$output .= param_html_output($key, $i);
		$i++;
	}
	return $output;
}

function param_html_output($array, $num){
	$name = 'custom_' . $num;
	$type = (isset($array['type']) ? $array['type'] : 'select');
	$output = '';

	switch ($type){
		case 'select':
		case 'checkbox':
			$output = param_out_is_checkbox($array, $name);
		break;
		case 'textarea':
		case 'input':
			$output = param_out_is_input($array, $name);
		break;
		case 'droplist':
		case 'List':
			$output = param_out_is_droplist($array, $name);
		break;
	}
	return $output;
}

function param_out_is_input($array, $name = ''){
	global $gp;

	$name = $name ? $name : $array['name'];
	$info = $array['infor'] ? $array['infor'] : '';
	$type = $array['type'] == 'textarea' ? 'textarea' : 'input';
	$value = $gp['pparam'][$name][0] ? $gp['pparam'][$name][0] : implode(", ", $array['value']);
	$value = '';

	$html = '<ul class="list_property"><li><label for="extra_' . $name . '" class="captain">' . $array[name] . '</label>';

	if ($type == 'input')
		$html .= '<input type="text" name="extra[' . $name . '][]" value="' . $value . '" id="extra_' . $name . '" />';
	else
		$html .= '<textarea name="extra[' . $name . '][]" id="extra_' . $name . '" class="yoiew">' . $value . '</textarea>';

	if ($info)
		$html .= '<p>' . $info . '</p>';
	$html .= '</li>';

	$html .= '<input type="hidden" name="extra[' . $name . '][name]" value="' . $array[name] . '" />';
	$html .= '</ul>';
	return $html;
}

function param_out_is_checkbox($array, $name = ''){
	global $gp;

	$name = $name ? $name : $array['name'];
	$param = explode("\n", $array['value']);
	$type = $array['type'] == 'select' ? 'radio' : 'checkbox';
	$info = $array['infor'] ? $array['infor'] : '';

	$html = '<ul class="list_property"><li><label class="captain">' . $array[name] . '</label>';
	$html .= '<ul>';

	foreach ($param as $key=>$value){
		$value = trim($value);
		$checked = in_array($value, $gp['pparam'][$name]) ? 'checked="checked"' : '';
		$html .= '<li><label for="extra_' . $name . $key . '">' . $value . '</label>';
		$html .= '<input name="extra[' . $name . '][]" type="' . $type . '" value="' . $value . '" id="extra_' . $name . $key . '" ' . $checked . ' class="checkbox" />';
		$html .= '</li>';
	}

	$html .= '</ul><div class="clear"></div>';

	if ($info)
		$html .= '<p>' . $info . '</p>';

	$html .= '</li>';

	if ($array['extra']){
		$html .= '<li><label>' . lang('shop/template', 'hoac_dien') . '</label>';
		$html .= '<input type="text" name="extra[' . $name . '][custom]" value="' . $gp['pparam'][$name]['custom'] . '" class="input_p"></li>';
	}

	$html .= '<input type="hidden" name="extra[' . $name . '][name]" value="' . $array[name] . '" />';
	$html .= '</ul>';

	return $html;
}

function param_out_is_droplist($array, $name = ''){
	global $gp;

	$name = $name ? $name : $array['name'];
	$param = explode("\n", $array['value']);
	$info = $array['infor'] ? $array['infor'] : '';

	$html = '<ul class="list_property"><li><label class="captain">' . $array[name] . '</label>';

	if ($array['type'] == 'droplist')
		$html .= '<select name="extra[' . $name . '][]">';
	else
		$html .= '<select multiple="multiple" size="7" name="extra[' . $name . '][]">';

	$html .= '<option value="">' . lang('shop/template', 'chon_') . $array[name] . '</option>';

	foreach ($param as $key=>$value){
		$value = trim($value);
		$selected = in_array($value, $gp['pparam'][$name]) ? 'selected="selected"' : '';
		$html .= '<option value="' . $value . '" ' . $selected . '>' . $value . '</option>';
	}

	$html .= '</select>';

	if ($info)
		$html .= '<p>' . $info . '</p>';

	$html .= '</li>';

	if ($array['extra']){
		$html .= '<li><label>' . lang('shop/template', 'hoac_dien') . '</label>';
		$html .= '<input type="text" name="extra[' . $name . '][custom]" value="' . $gp['pparam'][$name]['custom'] . '" class="input_p"></li>';
	}

	$html .= '<input type="hidden" name="extra[' . $name . '][name]" value="' . $array[name] . '" />';
	$html .= '</ul>';

	return $html;
}
/****************************************************************************************/
/*
 * Insert new product
 * */
function insert_product($data){
	$tablename = 'shop_products';
	$pid = DB::insert($tablename, $data, TRUE);
	return $pid;
}
/*
 * Update a product
 * */
function update_product($data, $condition){
	$tablename = 'shop_products';
	$res = DB::update($tablename, $data, $condition);
	return $res;
}

/*
 * Move image attach to image shop dir
 * */
function move_attach($attachs, $pid, $action = 'insert', $thumbfile = FALSE, $return = FALSE){
	global $_G;
	$dir_target = 'shop/';
	$dir_src = 'forum/';

	if ($action != 'insert'){
		$dir_target = 'forum/';
		$dir_src = 'shop/';
	}

	foreach ($attachs as $value){

		$aid = $value['aid'];
		$dir_server = $value['remote'] ? $_G['setting']['ftp']['attachdir'] : $_G['setting']['attachdir'];
		$dir_img = substr($value['attachment'], 0, 10);

		if ($value['remote']){
			$dir_server = '';
		}else{
			$dir = $_G['setting']['attachdir'] . $dir_target . $dir_img;
			$filename = $_G['setting']['attachdir'] . $dir_src . $value['attachment'];
			$target_file = $_G['setting']['attachdir'] . $dir_target . $value['attachment'];
			dmkdir($dir);
		}

		if (copy($filename, $target_file)){

			if ($thumbfile && $action == 'insert'){
				$thumbname = 'shop/' . $dir_img . $aid . '.thumb.jpg';
				pcreate_thumb($target_file, $thumbname, 140, 140, 'fixwr');
				$thumbname = 'shop/' . $dir_img . $aid . '.thumb.big.jpg';
				pcreate_thumb($target_file, $thumbname, 420, 420, 'fixwr');
			}

			punlink($value, $action, $thumbfile);

			if ($action == 'insert'){
				$data = $value;
				$data['pid'] = $pid;

				DB::insert('shop_products_attachment', $data);
				DB::delete('forum_attachment', "`aid`='$aid'", 1);
				DB::delete('forum_attachment_unused', "`aid`='$aid'", 1);
			}else{
				$data2['tid'] = 0;
				$data2['pid'] = 0;
				$data2['uid'] = $value['uid'];
				$data2['tableid'] = 127;
				$data2['downloads'] = 0;

				$aids = DB::insert('forum_attachment', $data2, true);

				$data['aid'] = $aids;
				$data['uid'] = $value['uid'];
				$data['dateline'] = $value['dateline'];
				$data['filename'] = $value['filename'];
				$data['filesize'] = $value['filesize'];
				$data['attachment'] = $value['attachment'];
				$data['remote'] = $value['remote'];
				$data['isimage'] = $value['isimage'];
				$data['width'] = $value['width'];
				$data['thumb'] = $value['thumb'];

				DB::insert('forum_attachment_unused', $data);

				DB::delete('shop_products_attachment', "`aid`='$aid'", 1);
			}
		}
	}

	if ($return){
		return $aids;
	}
}

function pcreate_thumb($filesrc, $filename, $w = 140, $h = 140, $type = 'fixwr'){
	require_once libfile('class/image');
	$img = new image();
	$img->Thumb($filesrc, $filename, $w, $h, $type);
}

/*
 * Truncate string
 * */
function truncate($str, $len, $charset = 'UTF-8'){
	$str = strip_tags($str);
	$str = htmlspecialchars($str);
	$str = html_entity_decode($str, ENT_QUOTES, $charset);

	if (mb_strlen($str, $charset) > $len){
		$arr = explode(' ', $stgr);
		$str = mb_substr($str, 0, $len, $charset);
		$arrRes = explode(' ', $str);
		$last = $arr[count($arrRes) - 1];
		unset($arr);
		if (strcasecmp($arrRes[count($arrRes) - 1], $last)){
			unset($arrRes[count($arrRes) - 1]);
		}
		return implode(' ', $arrRes) . ' ...';
	}
	return $str;
}

/*
 * For memcached
 * */
function get_memory_cached($key){
	if (memory('check')){
		$set = memory('get', $key);
		define($key, $set ? 1 : 0);
		if ($set){
			return $set;
		}
	}
	return array(
			'none'=>null
	);
}